<template>
  <div class="app-container">
    <div class="btn-container">
      <el-button type="primary" @click="openSave()">添加轮播图</el-button>
    </div>
    <el-table
      :height="tableHeight"
      header-cell-class-name="aa"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="50"
      />
      <el-table-column
        prop="categoryName"
        align="center"
        label="图片"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.productPath"
            fit="fit"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="创建时间"
      />
      <el-table-column
        prop="updateTime"
        align="center"
        label="更新时间"
      />
      <el-table-column
        fixed="right"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openUpdateHomeImg(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="removeHomeImg(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="upLoadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转链接" :label-width="formLabelWidth">
          <!--          <el-select v-model="form.region" placeholder="请选择活动区域" @change="changeType">-->
          <!--            <el-option label="商品" value="1" />-->
          <!--            <el-option label="文章" value="2" />-->
          <!--          </el-select>-->
          <el-select v-model="value" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.productId"
              :label="item.productName"
              :value="item.productId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="isUpdate" type="primary" @click="updateHomeImg()">确 定</el-button>
        <el-button v-else type="primary" @click="addHomeImg()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getHomeList, addHomeImg, updateHomeImg, removeHomeImg, getProductList } from '@/api/homeImg'
export default {
  name: 'Index',
  data() {
    return {
      id: '',
      upLoadUrl: process.env.VUE_APP_BASE_API + 'product-info/upload',
      isUpdate: false,
      title: '添加轮播图',
      tableHeight: window.innerHeight - 170,
      tableData: [],
      formLabelWidth: '100px',
      dialogFormVisible: false,
      imageUrl: '',
      form: {
        region: '',
        name: ''
      },
      options: [],
      value: ''
    }
  },
  mounted() {
    this.getHomeList()
    this.getProductList()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    getHomeList() {
      getHomeList().then(response => {
        this.tableData = response.data
      })
    },
    addHomeImg() {
      addHomeImg({ productPath: this.imageUrl, productId: this.value }).then(response => {
        this.$message({
          message: '添加成功',
          type: 'success'
        })
        this.getHomeList()
        this.dialogFormVisible = false
      })
    },
    openUpdateHomeImg(row) {
      this.isUpdate = true
      this.dialogFormVisible = true
      this.title = '编辑轮播图'
      this.imageUrl = row.productPath
      this.value = row.productId
      this.id = row.id
    },
    updateHomeImg() {
      const data = {
        id: this.id,
        productPath: this.imageUrl,
        productId: this.value
      }
      updateHomeImg(data).then(response => {
        this.$message({
          type: 'success',
          message: '编辑成功!'
        })
        this.getHomeList()
        this.dialogFormVisible = false
      })
    },
    removeHomeImg(id) {
      this.$confirm('确定删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeHomeImg(id).then(response => {
          this.getHomeList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getProductList() {
      getProductList().then(response => {
        this.options = response.data
      })
    },
    changeType(value) {
      if (value === '1') {
        this.getProductList()
      } else {
        this.options = []
      }
    },
    openSave() {
      this.dialogFormVisible = true
      this.isUpdate = false
      this.title = '添加轮播图'
      this.imageUrl = ''
      this.value = ''
    }
  }
}
</script>

<style lang="scss" scoped>
  .btn-container{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    margin-bottom: 10px;
  }
  .el-icon-plus .avatar-uploader-icon {
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader i{
    border: 1px dashed #d9d9d9!important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
